<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Order;
use app\model\OrderGoods;
use app\model\Member;
use app\model\Comment;

class CommentController extends Base
{
    public function getorder() 
    {

        $id = input("get.id");
        $data = Order::order_info($id);
        return $this->json(['data' => $data]);
    }
    public function add()
    {
        $Membermob = new Member;
        $memberinfo = $Membermob->getUserByWechat();

        $postJsonString = input("post.postJsonString");
        $images = input("post.images");
        if (!empty($images)) {
            $images = json_encode(explode(',', $images));
        }
        $postdata = json_decode($postJsonString, true);
        $level_store = $postdata['orderInfo']['store'][0]['level_store'];
        $level_store_specialty = $postdata['orderInfo']['store'][0]['level_store_specialty'];
        $level_store_service = $postdata['orderInfo']['store'][0]['level_store_service'];
        $level_technical = array();
        if (!empty($postdata['orderInfo']['staff'])) {
            foreach ($postdata['orderInfo']['staff'] as $k => $vo) {
                $level_technical[$k] = [$vo['id'] => $vo['level_technical']];
            }
        }
        foreach ($postdata['goods'] as $vo) {
            Comment::create([
                'weid' => weid(),
                'uid' => $memberinfo['id'],
                'nick_name' => $memberinfo['nickname'],
                'head_img_url' => $memberinfo['userpic'],
                'status' => 0,
                'order_id' => $vo['order_id'],
                'goods_id' => $vo['goods_id'],
                'level' => $vo['level'],
                'content' => $vo['content'],
                'images' => $images,
                'level_store' => $level_store,
                'level_store_specialty' => $level_store_specialty,
                'level_store_service' => $level_store_service,
                'level_technical' => json_encode($level_technical),
                'create_time' => time(),
                'update_time' => time()
            ]);
        }

        Order::update(['is_comment' => 1, 'id' => $postdata['goods'][0]['order_id']]);
        return $this->json(['msg' => '感谢您的评价', 'data' => $images, 'postdata' => $postdata]);
    }

    public function getgoodscomment()
    {

        $goods_id = input("get.goodsId");
        $where['goods_id'] = $goods_id;
        $where['status'] = 1;
        $data = Comment::where($where)->order('id asc')->paginate(getpage())->each(function ($item, $key) {
            $item['nick_name'] = substr_cut($item['nick_name']);
            $item['images'] = json_decode($item['images'], true);
            $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
            return $item;
        });

        return $this->json(['data' => $data]);
    }

    public function getgoods()
    {

        $goods_id = input("get.goodsId");
        $OrderGoods = new OrderGoods;

        $data =   $OrderGoods->getOrderGoods($goods_id);


        foreach ($data as &$vo) {
            $vo['level'] = 5;
            $vo['content'] = '';
        }
        return $this->json(['data' => $data]);
    }
}
